<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4185 2010-08-01 16:31:02Z huuphuoc $
 * @since		2.0.0
 */
?>
<?php
$this->headTitle($this->translate('privilege_list_page_title'));
$this->headLink()->prependStylesheet($this->APP_STATIC_SERVER.'/skins/admin/default/css/privilege.css');
$this->headScript()->appendFile($this->APP_STATIC_SERVER.'/js/jquery.json-2.3.min.js')
        ->appendFile($this->APP_STATIC_SERVER.'/js/phpjs/php.full.min.js');
?>
<div id="r-heading">
    <img src="<?php echo $this->APP_STATIC_SERVER . '/skins/admin/default/images/privilege-48x48.png'; ?>" alt=""/>
    <span><?php echo $this->translate('privilege_list_breadcrumb'); ?></span>
</div>

<div class="r-left" style="width: 20%; float: left;">
    <!-- List of modules -->
    <div class="box" id="module-list">
        <div class="b-title"><?php echo $this->translate('privilege_modules_list_title') ?></div>
        <div class="b-container">
            <ul>
            <?php foreach ($this->modules as $module) { ?>
                <li><?php echo $module; ?></li>
            <?php }?>
            </ul>
        </div>
    </div>
</div>
<?php
try{
?>

<div class="r-right" style="width: 78%; float: right;">
    <?php foreach ($this->modules as $module) { ?>
    <div id="<?php echo $module; ?>Resource" class="moduleResource">
    <?php 
        $result = $this->privilegeLoader()->getPrivileges($module);
        if ($result) {
            foreach ($result as $i => $item){
    ?>
        <div class="box privilege">
            <div class="b-title">
            <?php echo $item['resource']->description; ?>
            <?php
            if (isset($this->dbResources[$module . ':' . $item['resource']->controller_name])) {
                if ($this->allow('delete', 'resource')) {?>
                <a href="javascript: void(0);" rel="<?php echo $this->dbResources[$module . ':' . $item['resource']->controller_name]; ?>" class="resourceAction delete"><?php echo $this->translate('privilege_list_delete_resource'); ?></a>
                <?php }
            }else if ($this->allow('add', 'resource')) {?>
                <a href="javascript: void(0);" rel="<?php echo $module . ':' . $item['resource']->controller_name; ?>" class="resourceAction add" title="<?php echo $this->escape($item['resource']->description); ?>"><?php echo $this->translate('privilege_list_add_resource'); ?></a>
                <?php } ?>
            </div>
            <div class="b-container">
            <?php foreach ($item['privileges'] as $priv) { ?>
                <div class="row">
                    <?php echo $priv->description; ?>
                    <?php
                    if (isset($this->dbPrivileges[$priv->module_name . ':' . $priv->controller_name . ':' . $priv->name])) {
                        if ($this->allow('delete')) {?>
                    <a href="javascript: void(0);" class="privilegeAction delete" rel="<?php echo $this->dbPrivileges[ $priv->module_name . ':' . $priv->controller_name . ':' . $priv->name]; ?>">[ - ]</a>
                    <?php }
                    } else if ($this->allow('add')) {?>
                    <a href="javascript: void(0);" class="privilegeAction add" title="<?php echo $this->escape($priv->description); ?>" rel="<?php echo $priv->module_name . ':' . $priv->controller_name . ':' . $priv->name; ?>">[ + ]</a>
                    <?php
                    }?>
               </div>
            <?php }?>
            </div>
        </div>
        <?php
                if($i % 2 == 1){
        ?>
        <div class="clearfix"></div>
        <?php
                }
            }
        }else{
            echo $this->translate('privilege_list_module_has_not_resource');
        }?>
    </div>
    <?php } ?>
</div>

<script type="text/javascript">
$(document).ready(function() {
    /**
     * Init current #module-list li
     */
    $("#module-list li").eq(0).addClass("current");
    $(".moduleResource").eq(0).css("display", "block");
    /**
    * Load resource based on selected module
    */
    $("#module-list li").click(function() {
        $('.moduleResource').eq($("#module-list li.current").index()).hide();
        $("#module-list li.current").removeClass("current");
        $(this).addClass("current");
        $('.moduleResource').eq($(this).index()).show();
    });
    <?php if ($this->allow('add', 'resource') || $this->allow('delete', 'resource')) { ?>
  $('a.resourceAction').click(function() {
        var type = $(this).hasClass('delete') ? 'delete' : 'add';
        var self = this;
        $(this).html("<span class='loading'></span>");
        switch (type) {
              <?php if ($this->allow('add', 'resource')) {?>
              case 'add':
                    $.ajax({
                        url: '<?php echo $this->url(array(), 'core_resource_add'); ?>',
                        type: 'POST',
                        data: { mc: $(this).attr('rel'), description: $(this).attr('title') },
                        success: function(response) {
                            var result = $.evalJSON(response);
                            if(result.status == "ok"){
                                $(self).html("<?php echo $this->escape($this->translate('privilege_list_delete_resource')); ?>")
                                            .removeClass('add').addClass('delete')
                                            .attr('rel', result.id);
                                alert(sprintf('<?php echo addslashes($this->translate('privilege_list_add_resource_success')); ?>', $(self).attr('title')));
                            }else{
                                alert("<?php echo addslashes($this->translate('privilege_list_add_resource_error'))?>");
                            }
                        }
                    });
                    break;
              <?php }?>

              <?php if ($this->allow('delete', 'resource')) { ?>
              case 'delete':
                    $.ajax({
                          url: '<?php echo $this->url(array(), 'core_resource_delete'); ?>',
                          type: 'POST',
                          data: { id: $(this).attr('rel') },
                          success: function(response) {
                                var result = $.evalJSON(response);
                                if(result.status == "ok"){
                                    $(self).html("<?php echo $this->escape($this->translate('privilege_list_add_resource')); ?>")
                                                .removeClass('delete').addClass('add')
                                                .attr('rel', result.mc).attr('title', result.description);
                                    alert(sprintf('<?php echo addslashes($this->translate('privilege_list_delete_resource_success')); ?>', result.description));
                                }else{
                                    alert("<?php echo addslashes($this->translate('privilege_list_delete_resource_error'))?>;")
                                }

                          }
                    });
                    break;
              <?php }?>
              default: break;
            }
      });
      <?php } 
      //add and delete privileges
      if ($this->allow('add') || $this->allow('delete')) { ?>
	$('a.privilegeAction').click(function() {
		var type = $(this).hasClass("delete") ? "delete" : "add";
		var self = this;
            $(this).html("<span class='loading'></span>");
		switch (type) {
			<?php if ($this->allow('add')) {?>
			case 'add':
				/**
				 * Add privilege
				 */
				$.ajax({
					url: '<?php echo $this->url(array(), 'core_privilege_add'); ?>',
					type: 'POST',
					data: { mca: $(this).attr('rel'), description: $(this).attr('title') },
					success: function(response) {
						$(self).html('[ - ]').removeClass('add').addClass('delete').attr('rel', response);
                                    alert(sprintf('<?php echo addslashes($this->translate('privilege_list_add_success')); ?>', $(self).attr('title')));
						
					}
				});
				break;
			<?php } ?>

			<?php if ($this->allow('delete')) { ?>
			case 'delete':
				/**
				 * Delete privilege
				 */
				$.ajaxq('core_privilege', {
					url: '<?php echo $this->url(array(), 'core_privilege_delete'); ?>',
					type: 'POST',
					data: { id: $(this).attr('rel') },
					success: function(response) {
						response = $.evalJSON(response);
						$(self).html('[ + ]').removeClass('delete').addClass('add').attr('rel', response.mca).attr('title', response.description);
                                    alert(sprintf('<?php echo addslashes($this->translate('privilege_list_delete_success')); ?>', response.description));
						
					}
				});
				break;
			<?php }?>
                  default: break;
		}
	});
	<?php } ?>
});
</script>
<?php
} catch (Exception $ex){
    echo $ex->getMessage();

}
?>